package org.cskj.demo.controller;

import org.cskj.demo.pojo.User;
import org.cskj.demo.service.imp.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/user")
public class UserController {
    private final double size=8;
    @Autowired
    private UserService userService;
    @RequestMapping("/all")
    public List<User> getall(int page){
        System.out.println("getall");
        int count =userService.count();
        double pageMax=Math.ceil(count/size);

        //计算page是否有效
        page=page>(int)(pageMax)?(int)(pageMax):page;
        //获取数据
        List<User> users1 = userService.getall(page, (int) size);
        return users1;
    }
    @RequestMapping("/count")
    public int count(){
        int count =userService.count();
        return count;

    }
    @RequestMapping("/check")
    public User check(String name,String password){
        List<User> list = userService.check(name, password);
        System.out.println(list.size());
        if (list.size()==0){
            System.out.println("----");
            return new User();
        }else {
            return list.get(0);
        }
    }
    @RequestMapping("/insert")
    public String insert(User user){
        //true表示同意
        boolean f1 = userService.checkname(user.getUsname());
        boolean f2 = userService.checkcard(user.getUscard());
        boolean f3 = userService.checkphone(user.getUsphone());
        if (f1&&f2&&f3){
            //全部为true
            user.setUstype(1);
            int insert = userService.insert(user);
            if (insert>0){
                return "ok";
            }else {
                return "error";
            }
        }else {
            return "s";
        }
    }
    @RequestMapping("/checkname")
    public String checkName(String name){
        boolean checkname = userService.checkname(name);
        if (checkname){
            return "ok";
        }else {
            return "error";
        }
    }
    @RequestMapping("/checkphone")
    public String checkphone(String phone){
        boolean checkname = userService.checkphone(phone);
        if (checkname){
            return "ok";
        }else {
            return "error";
        }
    }
    @RequestMapping("/checkcard")
    public String checkcard(String card){
        boolean checkname = userService.checkcard(card);
        if (checkname){
            return "ok";
        }else {
            return "error";
        }
    }
    @RequestMapping("/update")
    public String update(User user){

            user.setUstype(1);
            int insert = userService.update(user);
            if (insert>0){
                return "ok";
            }else {
                return "error";
            }
    }
    @RequestMapping("/ban")
    public String ban(int usid){
        User user = userService.selectBykey(usid);
        user.setUstype(0);
        int insert = userService.update(user);
        if (insert>0){
            return "ok";
        }else {
            return "error";
        }
    }
    @RequestMapping("/key")
    public User selectBykey(String usid){
        System.out.println(usid);
        Integer value = Integer.valueOf(usid);
        User user = userService.selectBykey(value);
        return user;
    }


}
